function showInfo(msg, item) {
    var labelId = item.id + "_msg";
    document.getElementById(labelId).style.color = "#ff9800";
    document.getElementById(labelId).innerHTML = msg;
}

function showError(msg, item) {
    var labelId = item.id + "_msg";
    document.getElementById(labelId).style.color = "red";
    document.getElementById(labelId).innerHTML = msg;
    return 1;
}

function showInfoly(msg, item) {
    var labelId = item.id + "_msg";
    document.getElementById(labelId).style.color = "#8FA0AC";
    document.getElementById(labelId).innerHTML = msg;
}

function showOk(msg, item) {
    var labelId = item.id + "_msg";
    document.getElementById(labelId).style.color = "green";
    document.getElementById(labelId).innerHTML = msg;
}
function isEmpty(a) {
	return $(a).val() == "" || $.trim($(a).val()).length == 0;
}

function sel_changed() {
    var n = document.getElementById("select1");
    if (n.value === "-1") {
        showError("请选择商品所属分类", $s("select1"));
    } else {
        showOk("商品类别选择完毕", $s("select1"))
    }
}

function $s(id) {
    return document.getElementById(id);
}
function validate() {
    var result = 0;

    if (isEmpty("#goodsName")) {
        result = result + showError("商品名不能为空", $s("goodsName"));
    } else if ($('#goodsName_msg')[0].style.color == "red") {
        result = result + 1;
    }

    if (isEmpty("#unit")) {
        result = result + showError("单位不能为空", $s("unit"));
    } else if ($('#unit_msg')[0].style.color == "red") {
        result = result + 1;
    }

    if (isEmpty("#price")) {
        result = result + showError("价格不能为空", $s("price"));
    } else if ($('#price_msg')[0].style.color == "red") {
        result = result + 1;
    }
    if ($("#select1").val()=="-1") {
        result = result + showError("请选择商品所属分类", $s("select1"));
    }
    if ($('#pictureData_msg')[0].style.color == "red") {
        result = result + 1;
    }
    if (result === 0) {
        result = confirm("确定要提交吗");
        return result;
    } else {
        return false;
    }
}

$(function() {
	document.getElementById("select1").onchange = sel_changed;   //注意,不能写上()
	$("#goodsName").focus(function() {
		showInfo('2-20位非空白字符',this);
	}).blur(function () {
		var reg = /^[^\s]{2,20}$/;
		if(isEmpty(this)){
			showError("商品名不能为空", this);
		}else if (reg.test(this.value) === false) {
			showError("商品名格式错误",this);
		}else {
			showOk("验证通过", this)
		}
	});
	$("#unit").focus(function() {
		showInfo('1-10位非空白字符',this);
	}).blur(function () {
		var reg = /^[^\s]{1,10}$/;
		if(isEmpty(this)){
			showError("单位不能为空", this);
		}else if (reg.test(this.value) === false) {
			showError("单位格式错误",this);
		}else {
			showOk("验证通过", this)
		}
	});
	$("#price").focus(function() {
		showInfo('不能为空，以元为单位，可以是小数',this);
	}).blur(function () {
		var reg = /^\d+(\.\d+)?$/;
		if(isEmpty(this)){
			showError("价格不能为空", this);
		}else if (reg.test(this.value) === false) {
			showError("价格必须为正整数或小数",this);
		}else {
			showOk("验证通过", this)
		}
	});
	


})